import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:collection/collection.dart';
import 'package:finance_app/src/widgets/bottom_sheet/filter_payment_bottom_sheet.dart';

mixin FilterPaymentSortMixin<T> on BaseProvider<T> {
  List<String> currentSelectFilterList = [];

  List<AccountEntity> currentSelectFilterAccount = [];

  List<AccountEntity> allAccounts = [];

  Map<String, String> mapToKey = {
    'Money-In': 'in',
    'Money-Out': 'out',
    'Account Transfer': 'transfer',
  };

  int get accountId;

  String get rangeTime;

  int get filterListLength =>
      currentSelectFilterList.length +
      (rangeTime != null ? 1 : 0) +
      currentSelectFilterAccount.length;

  List get filterList => null;

  void onRequestRefresh();

  void unFocus();

  void onShowFilter() {
    unFocus();
    obtainContext.call((context) async {
      Map<String, dynamic> result = await showFilterPaymentBottomSheet(
        context,
        selectFilter: List.from(currentSelectFilterList),
        defaultFilter: filterList,
        selectBank: List.from(currentSelectFilterAccount),
        banks: allAccounts,
        isHideBankSelect: accountId != null,
      );
      List<String> resultStringFilter = result['select'];
      List<AccountEntity> resultBankFilter = result['bankSelect'];

      if (!DeepCollectionEquality()
              .equals(resultStringFilter, currentSelectFilterList) ||
          !DeepCollectionEquality()
              .equals(resultBankFilter, currentSelectFilterAccount)) {
        currentSelectFilterList = resultStringFilter;
        currentSelectFilterAccount = resultBankFilter;
        update();
        onRequestRefresh();
      }
    });
  }

  void onRemoveFilter(String value) {
    currentSelectFilterList.remove(value);
    update();
    onRequestRefresh();
  }

  void onRemoveFilterBank(AccountEntity value) {
    currentSelectFilterAccount.remove(value);
    update();
    onRequestRefresh();
  }
}
